const app = getApp();
const publicFun = require('/utils/public.js');
import { userNGODetail, userNGOTeamList, userNGOModify, userData } from '/utils/request'

Page({
  data: {
    ngoData: {},
    ngoTeamList: [],
    modifyAlert: false,
  },
  onLoad(options) {
    my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
    })
    if(options.ngoId){
      this.setData({
        ngoUserId: app.globalData.userData.ngoid,
        ngoId: options.ngoId,
        userType: app.globalData.userData.ngotype,
        teamid: app.globalData.userData.teamid,
        ngoTeamList: [],
        systemInfo: app.globalData.systemInfo
      })
      this.userNGODetail(options.ngoId);
      this.userNGOTeamList(options.ngoId);
    }
  },
  jumpNGOLIst() {
    my.redirectTo({
      url: '../myOrgan/myOrgan'
    })
  },
  openModifyAlert() {
    this.setData({
      modifyAlert: true,
      userNameList: [
        { id: '1', name: app.globalData.userData.name },
        { id: '2', name: app.globalData.userData.name+'@'+ app.globalData.userData.ngoname},
      ]
    });
  },
  getModifyId(e) {
    let data = e.currentTarget.dataset;
    this.setData({
      userType: data.id
    })
  },
  closeMask() {
    this.setData({
      modifyAlert: false,
    });
  },
  async NGOModify(e) {
    let that = this;
    let data = e.currentTarget.dataset;
    try {
      let params = {
        UserId: app.globalData.userid,
        ngoid: that.data.ngoId,
        type: that.data.userType,
        teamid: data.id ? data.id : 0
      }
      my.showLoading();
      const res = await userNGOModify(params)
      if (res.S == 1) {
        my.hideLoading();
        publicFun.showToast(res.M);
        app.getUserData();
        if(data.add == 'add'){ //加入隶属机构
          setTimeout(() => {
            that.setData({
              ngoUserId: app.globalData.userData.ngoid,
              teamid: app.globalData.userData.teamid
            });
          }, 500)
        }else{
          that.setData({
            modifyAlert: false,
          });
          setTimeout(() => {
            my.switchTab({
              url: '/pages/my/my',
            })
          }, 500);
        }

      }else{
        my.hideLoading();
        publicFun.showToast(res.M);
      }
    } catch (e) {
      console.log(e)
    }
  },
  async userNGODetail(ngoId) {
    let that = this;
    try {
      let params = {
        id: ngoId
      }
      my.showLoading();
      const res = await userNGODetail(params)
      if (res.S == 1) {
        my.hideLoading();
        let str = publicFun.Base64.decode(res.Info);
        var reg=/<.+">|<div>|<\/.+>|&nbsp;|&rdquo;|&ldquo;|<\/br>|<p>|<strong>|<b>/g;
        str=str.replace(reg,' ');

        let data = {
          Name: res.Name,
          Info: str
        }
        that.setData({
          ngoData: data
        })
      }else{
        my.hideLoading();
        publicFun.showToast(res.M);
      }
    } catch (e) {
      console.log(e)
    }
  },
  async userNGOTeamList(ngoId) {
    let that = this;
    try {
      let params = {
        ngoid: ngoId
      }
      my.showLoading();
      const res = await userNGOTeamList(params)
      if (res.S == 1) {
        my.hideLoading();
        if(res.L.length > 0){
          res.L.forEach((item) => {   
            that.data.ngoTeamList.push(item);
          });
          that.setData({
            ngoTeamList: that.data.ngoTeamList
          })
        }
      }else{
        my.hideLoading();
        publicFun.showToast(res.M);
      }
    } catch (e) {
      console.log(e)
    }
  },
});
